package com.hdoit.kylin.logical.studentManager.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.hdoit.kylin.logical.studentManager.dao.IReadDocumentDao;
import com.hdoit.kylin.logical.studentManager.model.ReadDocument;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.viewmodel.postgraduate.ReadDocumentSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;

/**
 * @author anshengqiang
 * @email
 * @createtime 2017年07月26日 16:13:52
 * @comment
 */
@Component
public class ReadDocumentDaoImpl extends SuperDao<ReadDocument> implements IReadDocumentDao {

	@Override
	public int queryCount(ReadDocumentSearch search) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT student_read_document.id) as count")
				.from(ReadDocument.class, "student_read_document").leftJoin(Student.class, "student")
				.on("student_read_document.employeeId", "student.employeeId")
				.andWhere("student.employeeId IS NOT NULL").setMainTableAlias("student_read_document");
		return queryCount(finderExpression, search);
	}

	@Override
	public List<ReadDocument> queryList(ReadDocumentSearch search, PagerModel pagerModel) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("student_read_document.*", "student.studentName as employeeName")
				.from(ReadDocument.class, "student_read_document")
				.leftJoin(Student.class, "student")
				.on("student_read_document.employeeId", "student.employeeId")
				.addOrder("readDate", OrderEnum.desc);
		finderExpression.addOrderInfoList(getOrderInfoList());
		return queryList(finderExpression, search, pagerModel);
	}

	@Override
	public List<ReadDocument> queryListByStudentId(String studentId) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("student_read_document.*", "student.employeeName as studentName")
				.from(Student.class, "student")
				.leftJoin(ReadDocument.class, "student_read_document")
				.on("student_read_document.employeeId", "student.employeeId")
				.andWhere("student.employeeId=#{studentId}")
				.setParam("studentId", studentId)
				.addOrder("readDate", OrderEnum.desc);
		finderExpression.addOrderInfoList(getOrderInfoList());
		return queryList(finderExpression);
	}
}
